﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ZX.WebApp;

namespace ZX.WebApp.Crm.BasePages
{
	public partial class Tooltip : ZX.Web.UI.Controls.BasePage
	{
        string worktaskInsId = "";
        ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (GetStringFromQueryString("getMSG", "N") == "Y")
                {
                    getMSG();
                }
                else
                {
                    InitData();
                }
            }
        }
        private void getMSG()
        {
            if (Request["WorkTaskInstanceId"] != null)
            {
                string title = "";
                string TaskToWhoMsg = "";
                string ResultMsg = "";
                string transmitId = Session["transmitId"] == null ? string.Empty : Session["transmitId"].ToString();  

                worktaskInsId = Request["WorkTaskInstanceId"].ToString();
                TaskToWhoMsg = client.GetTaskToWhoMsg(worktaskInsId);
                ResultMsg = client.GetResultMsg(worktaskInsId);
                DataTable dt = client.GetWorkTaskInstanceView(Request["WorkTaskInstanceId"].ToString());
    
                title = "操作结果:" + ResultMsg;
  
                if (TaskToWhoMsg.Length <= 0)
                {
                    TaskToWhoMsg = "未提交至任何人,请检查流程模板和组织机构配置是否正确!";
                    if (ResultMsg == WorkFlowConst.WorkflowOverMsg)//流程结束
                    {
                        TaskToWhoMsg = "流程结束!";
                    }
                    if (ResultMsg == WorkFlowConst.TaskBackMsg)
                    {
                        TaskToWhoMsg = WorkFlowConst.TaskBackMsg;
                    }
                }

                else
                {
                    if (!string.IsNullOrEmpty(transmitId))
                    {
                        string[] transmituser = transmitId.Split(',');
                        string[] username = TaskToWhoMsg.Split(',');
                        TaskToWhoMsg = "";
                        for (int j = 0; j < transmituser.Length; j++)
                        {
                            for (int i = 0; i < username.Length; i++)
                            {
                                if (transmituser[j].ToString() == username[i].ToString())
                                {
                                    TaskToWhoMsg += username[i].ToString();
                                }
                            }
                        }
                    }

                    Session["transmitId"] = null;
                    TaskToWhoMsg = "成功提交至:" + TaskToWhoMsg + "。你已完成该任务处理,可以关闭该窗口。";
                }
                //---------------------------todo zhuyun 20121012--------------------------//
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["OperType"].ToString() == "12")
                    {
                        TaskToWhoMsg = "已成功提交,你已完成该任务处理,可以关闭该窗口。";
                    }
                }
                if (title.Contains("退回"))
                {
                    TaskToWhoMsg = "成功退回至提交人！";
                }
                Response.Write(TaskToWhoMsg);
            }
            Response.End();

        }
        private void InitData()
        {
            if (Request["WorkTaskInstanceId"] != null)
            {
                string title = "";
                string TaskToWhoMsg = "";
                string ResultMsg = "";
                //bool workflowos = false;
                //string assignUserId = ConfigurationManager.AppSettings["workflowassign"];
                string transmitId = Session["transmitId"] == null ? string.Empty : Session["transmitId"].ToString();    //todo zhuyun

                worktaskInsId = Request["WorkTaskInstanceId"].ToString();
                TaskToWhoMsg = client.GetTaskToWhoMsg(worktaskInsId);
                ResultMsg = client.GetResultMsg(worktaskInsId);
                DataTable dt = client.GetWorkTaskInstanceView(Request["WorkTaskInstanceId"].ToString()); 
                //if (string.IsNullOrEmpty(TaskToWhoMsg) && !ResultMsg.Contains("退回"))
                //{
                //    workflowos = GetTableValue(worktaskInsId, assignUserId);
                //}
                title = "操作结果:" + ResultMsg;
                //if (workflowos)
                //{
                //    TaskToWhoMsg = "成功提交,你已完成该任务处理,可以关闭该窗口。";
                //}
                //else
                //{
                if (TaskToWhoMsg.Length <= 0)
                {
                    TaskToWhoMsg = "未提交至任何人,请检查流程模板和组织机构配置是否正确!";
                    if (ResultMsg == WorkFlowConst.WorkflowOverMsg)//流程结束
                    {
                        TaskToWhoMsg = "流程结束!";
                    }
                    if (ResultMsg == WorkFlowConst.TaskBackMsg)
                    {
                        TaskToWhoMsg = WorkFlowConst.TaskBackMsg;
                    }
                }

                else
                {
                    if (!string.IsNullOrEmpty(transmitId))
                    {
                        string[] transmituser = transmitId.Split(',');
                        string[] username = TaskToWhoMsg.Split(',');
                        TaskToWhoMsg = "";
                        for (int j = 0; j < transmituser.Length; j++)
                        {
                            for (int i = 0; i < username.Length; i++)
                            {
                                if (transmituser[j].ToString() == username[i].ToString())
                                {
                                    TaskToWhoMsg += username[i].ToString();
                                }
                            }
                        }
                    }

                    Session["transmitId"] = null;
                    TaskToWhoMsg = "成功提交至:" + TaskToWhoMsg + "。你已完成该任务处理,可以关闭该窗口。";
                }
                //---------------------------todo zhuyun 20121012--------------------------//
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["OperType"].ToString() == "12")
                    {
                        TaskToWhoMsg = "已成功提交,你已完成该任务处理,可以关闭该窗口。";
                    }
                }
                if (title.Contains("退回"))
                {
                    TaskToWhoMsg = "成功退回至提交人！";
                }
                
                

                System.Text.StringBuilder builder = new System.Text.StringBuilder();
                builder.Append("<script>\r\n");
                builder.Append("var aa=setTimeout('Finish();',1000);\r\n");
                builder.Append("function Finish(){\r\n");
                builder.Append("document.all('Image2').src='../images/home.gif';\r\n");
                builder.Append("document.all('" + this.lbTitle.ClientID + "').innerText='" + title + "';\r\n");
                builder.Append("document.all('" + this.lbDescription.ClientID + "').innerText='" + TaskToWhoMsg + "';\r\n");
                //builder.Append("alert('ok');\r\n");
                builder.Append("clearTimeout(aa);\r\n");
                builder.Append("}\r\n");
                builder.Append("</script>\r\n");
                this.Page.RegisterClientScriptBlock("test", builder.ToString());
            }
        }
        //public bool GetTableValue(string worktaskInsId,string assignUserId)
        //{
        //        DataTable dt = client.GetWorkTaskInstanceView(worktaskInsId);
        //        string OperatorInstanceId = "";
        //        if (dt.Rows.Count > 0)
        //        {
        //            OperatorInstanceId = dt.Rows[0]["OperatorInstanceId"].ToString();
        //        }
        //        if (!string.IsNullOrEmpty(OperatorInstanceId))
        //        {
        //            ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
        //            string UserId = U.LoginName.ToString();
        //            //client.TaskClaim(U.LoginName.ToString(), OperatorInstanceId);  
        //            //string assignUserId=ConfigurationManager.AppSettings["workflow"].ToString();
        //            if (UserId != assignUserId)
        //            {
        //                client.WorkTaskAssign(UserId, assignUserId, OperatorInstanceId);
        //                return true;
        //            }
        //        }
        //        return false;
                
        //}
        protected void Button1_Click1(object sender, EventArgs e)
        {
            string title = "ok";
            string description = "yes";
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script>\r\n");
            builder.Append("var aba=setTimeout('Finish();',1000);\r\n");
            builder.Append("function Finish(){\r\n");
            builder.Append("document.all('Image2').src='../images/home.gif';\r\n");
            builder.Append("document.all('" + this.lbTitle.ClientID + "').innerText='" + title + "';\r\n");
            builder.Append("document.all('" + this.lbDescription.ClientID + "').innerText='" + description + "';\r\n");
            // builder.Append("alert('ok');\r\n");
            builder.Append("clearTimeout(aba);\r\n");
            builder.Append("}\r\n");
            builder.Append("</script>\r\n");
            this.Page.RegisterClientScriptBlock("test", builder.ToString());
        }
	}
}